<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title>Redis配置tcp-keepalive和timeout参数测试  - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="Redis配置tcp-keepalive和timeout参数测试 ">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content="Redis配置tcp-keepalive和timeout参数测试  - 天地维杰网" />
    <meta name="description" content="redis server 可以断开 KA 丢失的连接或空闲的连接。这将使 Redis服务免受 TCP 连接耗尽问题的影响。" />
     
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis-tcp-keepalive-and-timeout-configuration-test/" itemprop="url">
        Redis配置tcp-keepalive和timeout参数测试 
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="2022-10-21">
    2022-10-21
</time>
</span> 
      

  <span class="post-category" >
  &nbsp; | &nbsp;
  <span class="post-meta-item-icon">
    <i class="fa fa-folder-o"></i>
  </span>
  <span class="post-meta-item-text">分类：</span>
  
    <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
      <a href="http://www.shutdown.cn/categories/redis" itemprop="url" rel="index">
        <span itemprop="name">redis</span>
      </a>
      &nbsp; 
    </span>
  
</span>


      
 <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">原创</span>
</span>

       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">1113 字 ~3分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    <p>启动redis实例</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">nohup ./redis-server <span style="color:#000;font-weight:bold">&amp;</span></code></pre></div>
<p>使用redis-cli连接并设置tcp-keepalive属性和timeout属性，两个属性都是0，redis默认是不开启的。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 logs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 6379</span>
127.0.0.1:6379&gt; config get tcp-keepalive
1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;tcp-keepalive&#34;</span>
2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;0&#34;</span>
127.0.0.1:6379&gt; config get timeout
1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;timeout&#34;</span>
2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;0&#34;</span>
127.0.0.1:6379&gt;</code></pre></div>
<p>使用config set 命令分别设置tcp-keepalive属性和timeout属性的值，或者在redis.conf配置文件中修改这两个属性，要注意修改配置文件需要重启redis服务。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">127.0.0.1:6379&gt; config <span style="color:#204a87">set</span> tcp-keepalive <span style="color:#0000cf;font-weight:bold">60</span>
OK
127.0.0.1:6379&gt; config get tcp-keepalive
1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;tcp-keepalive&#34;</span>
2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;60&#34;</span>
127.0.0.1:6379&gt; config <span style="color:#204a87">set</span> timeout <span style="color:#0000cf;font-weight:bold">300</span>
OK
127.0.0.1:6379&gt; config get timeout
1<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;timeout&#34;</span>
2<span style="color:#ce5c00;font-weight:bold">)</span> <span style="color:#4e9a06">&#34;300&#34;</span>
127.0.0.1:6379&gt;</code></pre></div>
<p>使用redis-cli从其他服务器远程连接这个redis实例（从其他服务器便于观察）</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic"># 其他服务器 10.4.7.1 redis-cli 连接</span>

 ~ % /Users/domino/redis/redis-3.2.3/src/redis-cli -h 10.4.7.106 -p <span style="color:#0000cf;font-weight:bold">6379</span>

<span style="color:#8f5902;font-style:italic"># redis实例服务器 通过client list命令查看客户端连接</span>

127.0.0.1:6379&gt; client list
<span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">44</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span>127.0.0.1:37536 <span style="color:#000">laddr</span><span style="color:#ce5c00;font-weight:bold">=</span>127.0.0.1:6379 <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">8</span> <span style="color:#000">name</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">age</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">369</span> <span style="color:#000">idle</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span>N <span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">sub</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">psub</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">multi</span><span style="color:#ce5c00;font-weight:bold">=</span>-1 <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">26</span> qbuf-free<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">40928</span> argv-mem<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> tot-mem<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">61466</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span>r <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span>client <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>default <span style="color:#000">redir</span><span style="color:#ce5c00;font-weight:bold">=</span>-1

<span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">45</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span>10.4.7.1:55544 <span style="color:#000">laddr</span><span style="color:#ce5c00;font-weight:bold">=</span>10.4.7.106:6379 <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">9</span> <span style="color:#000">name</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">age</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">142</span> <span style="color:#000">idle</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">142</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span>N <span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">sub</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">psub</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">multi</span><span style="color:#ce5c00;font-weight:bold">=</span>-1 <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> qbuf-free<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> argv-mem<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> tot-mem<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">20496</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span>r <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#204a87">command</span> <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>default <span style="color:#000">redir</span><span style="color:#ce5c00;font-weight:bold">=</span>-1

<span style="color:#8f5902;font-style:italic"># 可以看到一个连接是本机redis-cli连接，另一个是其他服务器的连接</span></code></pre></div>
<p>通过netstat命令观察连接情况</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># netstat -nap | egrep -i 6379 | egrep -i \.7\.1\:</span>
tcp        <span style="color:#0000cf;font-weight:bold">0</span>      <span style="color:#0000cf;font-weight:bold">0</span> 10.4.7.106:6379         10.4.7.1:55544          ESTABLISHED 10083/./redis-serve</code></pre></div>
<p>通过tcpdump查看</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#8f5902;font-style:italic"># 先通过ifconfig查看网卡，可见使用的是ens33</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 logs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># ifconfig</span>
ens33: <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span>4163&lt;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu <span style="color:#0000cf;font-weight:bold">1500</span>
        inet 10.4.7.106  netmask 255.255.252.0  broadcast 10.4.7.255
        inet6 fe80::f002:29f9:88c3:a1a2  prefixlen <span style="color:#0000cf;font-weight:bold">64</span>  scopeid 0x20&lt;link&gt;
        ether 00:0c:29:54:70:ec  txqueuelen <span style="color:#0000cf;font-weight:bold">1000</span>  <span style="color:#ce5c00;font-weight:bold">(</span>Ethernet<span style="color:#ce5c00;font-weight:bold">)</span>
        RX packets <span style="color:#0000cf;font-weight:bold">27209007</span>  bytes <span style="color:#0000cf;font-weight:bold">15499242089</span> <span style="color:#ce5c00;font-weight:bold">(</span>14.4 GiB<span style="color:#ce5c00;font-weight:bold">)</span>
        RX errors <span style="color:#0000cf;font-weight:bold">0</span>  dropped <span style="color:#0000cf;font-weight:bold">0</span>  overruns <span style="color:#0000cf;font-weight:bold">0</span>  frame <span style="color:#0000cf;font-weight:bold">0</span>
        TX packets <span style="color:#0000cf;font-weight:bold">23136520</span>  bytes <span style="color:#0000cf;font-weight:bold">2780588807</span> <span style="color:#ce5c00;font-weight:bold">(</span>2.5 GiB<span style="color:#ce5c00;font-weight:bold">)</span>
        TX errors <span style="color:#0000cf;font-weight:bold">0</span>  dropped <span style="color:#0000cf;font-weight:bold">0</span> overruns <span style="color:#0000cf;font-weight:bold">0</span>  carrier <span style="color:#0000cf;font-weight:bold">0</span>  collisions <span style="color:#0000cf;font-weight:bold">0</span>

<span style="color:#8f5902;font-style:italic"># 使用tcpdump 查看 tcp端口6379的往来通信，这里我把redis-server 的tcp-keepalive配置改为了5秒，所以交互很频繁，说明tcp-keepalive生效。</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># tcpdump -nei ens33 &#34;tcp port 6379&#34;</span>
tcpdump: verbose output suppressed, use -v or -vv <span style="color:#204a87;font-weight:bold">for</span> full protocol decode
listening on ens33, link-type EN10MB <span style="color:#ce5c00;font-weight:bold">(</span>Ethernet<span style="color:#ce5c00;font-weight:bold">)</span>, capture size <span style="color:#0000cf;font-weight:bold">262144</span> bytes
<span style="color:#8f5902;font-style:italic"># 可以看到 redis-server向 10.4.7.1的客户端发送心跳检测</span>
17:05:07.673530 00:0c:29:54:70:ec &gt; fa:ff:c2:d5:49:64, ethertype IPv4 <span style="color:#ce5c00;font-weight:bold">(</span>0x0800<span style="color:#ce5c00;font-weight:bold">)</span>, length 66: 10.4.7.106.6379 &gt; 10.4.7.1.56667: Flags <span style="color:#ce5c00;font-weight:bold">[</span>.<span style="color:#ce5c00;font-weight:bold">]</span>, ack 250714536, win 227, options <span style="color:#ce5c00;font-weight:bold">[</span>nop,nop,TS val <span style="color:#0000cf;font-weight:bold">68425312</span> ecr 2278728736<span style="color:#ce5c00;font-weight:bold">]</span>, length <span style="color:#0000cf;font-weight:bold">0</span>
<span style="color:#8f5902;font-style:italic"># 10.4.7.1客户端向 redis-server进行返回</span>
17:05:07.673882 fa:ff:c2:d5:49:64 &gt; 00:0c:29:54:70:ec, ethertype IPv4 <span style="color:#ce5c00;font-weight:bold">(</span>0x0800<span style="color:#ce5c00;font-weight:bold">)</span>, length 66: 10.4.7.1.56667 &gt; 10.4.7.106.6379: Flags <span style="color:#ce5c00;font-weight:bold">[</span>.<span style="color:#ce5c00;font-weight:bold">]</span>, ack 1, win 2058, options <span style="color:#ce5c00;font-weight:bold">[</span>nop,nop,TS val <span style="color:#0000cf;font-weight:bold">2278733740</span> ecr 68415295<span style="color:#ce5c00;font-weight:bold">]</span>, length <span style="color:#0000cf;font-weight:bold">0</span>
…………</code></pre></div>
<p>在客户端空闲超过timeout配置设置的300秒以后，查看client list，可以看到空闲的远程客户端已经被不在了。tcpdump也不会有新的结果展示。说明timeout参数也生效了。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 logs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># netstat -naplo | egrep -i 6379 | egrep -i \.7\.1\:</span>
<span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 ~<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># tcpdump -nei ens33 &#34;tcp port 6379&#34;</span>
tcpdump: verbose output suppressed, use -v or -vv <span style="color:#204a87;font-weight:bold">for</span> full protocol decode
listening on ens33, link-type EN10MB <span style="color:#ce5c00;font-weight:bold">(</span>Ethernet<span style="color:#ce5c00;font-weight:bold">)</span>, capture size <span style="color:#0000cf;font-weight:bold">262144</span> bytes
<span style="color:#ce5c00;font-weight:bold">[</span>root@redis-7-106 logs<span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#8f5902;font-style:italic"># redis-cli -p 6379</span>
127.0.0.1:6379&gt; client list
<span style="color:#000">id</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">99</span> <span style="color:#000">addr</span><span style="color:#ce5c00;font-weight:bold">=</span>127.0.0.1:37546 <span style="color:#000">laddr</span><span style="color:#ce5c00;font-weight:bold">=</span>127.0.0.1:6379 <span style="color:#000">fd</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">8</span> <span style="color:#000">name</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">age</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">356</span> <span style="color:#000">idle</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">flags</span><span style="color:#ce5c00;font-weight:bold">=</span>N <span style="color:#000">db</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">sub</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">psub</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">multi</span><span style="color:#ce5c00;font-weight:bold">=</span>-1 <span style="color:#000">qbuf</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">26</span> qbuf-free<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">40928</span> argv-mem<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">10</span> <span style="color:#000">obl</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">oll</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000">omem</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">0</span> tot-mem<span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#0000cf;font-weight:bold">61466</span> <span style="color:#000">events</span><span style="color:#ce5c00;font-weight:bold">=</span>r <span style="color:#000">cmd</span><span style="color:#ce5c00;font-weight:bold">=</span>client <span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">=</span>default <span style="color:#000">redir</span><span style="color:#ce5c00;font-weight:bold">=</span>-1</code></pre></div>
<p>redis server 可以断开 KA 丢失的连接或空闲的连接。这将使 Redis服务免受 TCP 连接耗尽问题的影响。</p>

<p>参考文档：<a href="https://supportportal.juniper.net/s/article/Contrail-How-to-turn-on-TCP-KA-keepalive-and-idle-timeout-for-redis-server?language=en_US">https://supportportal.juniper.net/s/article/Contrail-How-to-turn-on-TCP-KA-keepalive-and-idle-timeout-for-redis-server?language=en_US</a></p>

    </div>
    <footer class="post-footer">
     
 
<div class="post-tags">     
     
    <a href="http://www.shutdown.cn/tags/redis" rel="tag" title="redis">#redis#</a>
    
    <a href="http://www.shutdown.cn/tags/tcp-keepalive" rel="tag" title="tcp-keepalive">#tcp-keepalive#</a>
    
    <a href="http://www.shutdown.cn/tags/timeout" rel="tag" title="timeout">#timeout#</a>
    
    <a href="http://www.shutdown.cn/tags/connection" rel="tag" title="connection">#connection#</a>
    
</div>



     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis-source-code-learn-of-total-sight/" rel="next" title="Redis源码解析-全览">
        <i class="fa fa-chevron-left"></i> Redis源码解析-全览
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/nacos-apollo-spring-cloud-config-different-and-choose/" rel="prev" title="Nacos、Apollo、Config配置中心如何选型？这10个维度告诉你！">
        Nacos、Apollo、Config配置中心如何选型？这10个维度告诉你！ <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

  <ul class="sidebar-nav motion-element">
    <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
      文章目录
    </li>
    <li class="sidebar-nav-overview" data-target="site-overview">
      站点概览
    </li>
  </ul>

    <section class="site-overview sidebar-panel ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
<section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
    <div class="post-toc">
        <div class="post-toc-content"></div>
    </div>
</section>

  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>